package com.personal.revenant.oulewaimai.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.os.Build;
import android.util.Log;

import androidx.annotation.RequiresApi;

import com.igexin.sdk.GTIntentService;
import com.igexin.sdk.message.GTCmdMessage;
import com.igexin.sdk.message.GTNotificationMessage;
import com.igexin.sdk.message.GTTransmitMessage;
import com.personal.revenant.oulewaimai.R;
import com.personal.revenant.oulewaimai.bean.C;
import com.personal.revenant.oulewaimai.bean.Event;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Timer;
import java.util.TimerTask;


public class DemoIntentService extends GTIntentService {
    private GetClientId getClientId;

    private AssetManager aManager;
    private SoundPool mSoundPool = null;
    private HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();

    public DemoIntentService(GetClientId getClientId) {
        this.getClientId = getClientId;
    }

    public DemoIntentService() {

    }

    public interface GetClientId {
        void GetClientId(Object params);
    }

    @Override
    public void onReceiveClientId(Context context, String clientid) {
        Log.e(TAG, "onReceiveClientId -> " + "clientid = " + clientid);
        Event event = new Event(C.EventCode.A, clientid);
        EventBus.getDefault().post(event);
        getClientId.GetClientId(clientid);

    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceiveServicePid(Context context, int i) {

        AudioAttributes abs = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        mSoundPool = new SoundPool.Builder()
                .setMaxStreams(100)   //设置允许同时播放的流的最大值
                .setAudioAttributes(abs)   //完全可以设置为null
                .build();
        //设置最多可容纳5个音频流，音频的品质为5
        //mSoundPool = new SoundPool(5, AudioManager.STREAM_MUSIC, 5);

    }

    @Override
    public void onReceiveMessageData(Context context, GTTransmitMessage gtTransmitMessage) {
//        SoundPool mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100);
//        HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();
//        soundID.put(1, mSoundPool.load(this, R.raw.shop, 1));
//        soundID.put(2, mSoundPool.load(this, R.raw.qishi, 2));
//        soundID.put(3, mSoundPool.load(this, R.raw.shopjiedan, 3));
        MediaPlayer player = MediaPlayer.create(this, R.raw.shop);
        MediaPlayer player2 = MediaPlayer.create(this, R.raw.qishi);
        MediaPlayer player3 = MediaPlayer.create(this, R.raw.shopjiedan);

        Timer timer = new Timer();// 实例化Timer类
        Context that = this;
        timer.schedule(new TimerTask() {
            public void run() {
                System.out.println("退出");
                String st = new String(gtTransmitMessage.getPayload());
                if (st.equals("您有一个新的订单")) {
//                    AudioManager mgr = (AudioManager)that.getSystemService(Context.AUDIO_SERVICE);
//                    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
//                    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//                    float volume = streamVolumeCurrent/streamVolumeMax;
//                    mSoundPool.play(soundID.get(1), volume, volume, 1, 0, 1f);
//                    //参数：1、Map中取值   2、当前音量     3、最大音量  4、优先级   5、重播次数   6、播放速度
                    player.start();
//            mSoundPool.play(soundID.get(1), 1, 1, 0, 0, 1);
                } else if (st.equals("小鸡外卖有新订单啦，请前去抢单！")) {
//                    AudioManager mgr = (AudioManager)that.getSystemService(Context.AUDIO_SERVICE);
//                    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
//                    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//                    float volume = streamVolumeCurrent/streamVolumeMax;
                    player2.start();
//                    mSoundPool.play(soundID.get(2), volume, volume, 1, 0, 1f);
                } else if (st.equals("商家已经接单，请稍等")) {
//                    AudioManager mgr = (AudioManager) that.getSystemService(Context.AUDIO_SERVICE);
//                    float streamVolumeCurrent = mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
//                    float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//                    float volume = streamVolumeCurrent / streamVolumeMax;
//
                    player3.start();

//                    mSoundPool.play(soundID.get(3), volume, volume, 1, 0, 1f);
//            mSoundPool.play(soundID.get(3), 1, 1, 0, 0, 1);

                }
            }
        }, 1500);// 这里百毫秒

//        else if(st.equals("小鸡外卖有新订单啦，请前去抢单！")){
//            soundPool= new SoundPool(10, AudioManager.STREAM_SYSTEM,5);
//            soundPool.load(this, R.raw.商家提示音,1);
//            soundPool.play(1,1, 1, 0, 0, 1);
//        }
    }

    @Override
    public void onReceiveOnlineState(Context context, boolean b) {

    }

    @Override
    public void onReceiveCommandResult(Context context, GTCmdMessage gtCmdMessage) {

    }

    @Override
    public void onNotificationMessageArrived(Context context, GTNotificationMessage gtNotificationMessage) {

    }

    @Override
    public void onNotificationMessageClicked(Context context, GTNotificationMessage gtNotificationMessage) {

    }


}